/*
 * 文件名：ShoppingCarController.java
 * 版权：
 * 描述：TODO
 * 修改人：zuo liubo
 * 修改时间：2013-2-4 下午7:25:51
 */
package com.scpii.api.controller.ec;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.Constants;
import com.scpii.api.common.EcConstants;
import com.scpii.api.common.base.BaseController;
import com.scpii.api.common.exception.EcException;
import com.scpii.api.common.exception.UserIdDisaccordException;
import com.scpii.api.domain.ec.Product;
import com.scpii.api.domain.ec.ShoppingCar;
import com.scpii.api.service.ec.ShoppingCarService;
import com.scpii.api.util.PrincipalUtil;

/**
 * @description 购物车controller类
 * @author zuo liubo
 * @date 2013-2-4
 */

@Controller
@RequestMapping("/ec/shoppingCar")
public class ShoppingCarController extends BaseController {

	private Log logger = LogFactory.getLog(ShoppingCarController.class);
	@Resource
	private ShoppingCarService shoppingCarService;

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertProductInCar(@RequestParam("productId") String productId,ShoppingCar car) {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		car.setUserId(_userId);
		car.setCreateBy(_userId + "");
		car.setCreateDt(new Date());
		Product p = new Product(); 
		p.setProductId(Long.parseLong(productId));
		car.setProduct(p);
		Long  shoppingCarId = shoppingCarService.insert(car);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("shoppingCarId", shoppingCarId);
		return map;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public List<ShoppingCar> selectCarByUserId(ShoppingCar car) {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		car.setUserId(_userId);
		logger.info(car);
		List<ShoppingCar> carList = shoppingCarService.selectShoppingCarByUserId(car);
		return carList;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/{shoppingCarId}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String,Object> updateShoppingCarById(ShoppingCar car) throws Exception {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		if(car.getQuantity() <= 0 ){
			throw new EcException(EcConstants.ERROR_INFO.LESS_THAN_ZERO);
		}
		car.setUserId(_userId);
		car.setUpdateBy(_userId + "");
		car.setUpdateDt(new Date());
		Long shoppingCarId = shoppingCarService.updateShoppingCarById(car);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("shoppingCarId", shoppingCarId);
		return map;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/{shoppingCarId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Boolean deleteShoppingCarById(@PathVariable("shoppingCarId") String shoppingCarId, Long userId,
			String otherShoppingCarId)throws Exception {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		if (!_userId.equals(userId)) {
			throw new UserIdDisaccordException();
		}

		List<Long> carIdList = new ArrayList<Long>();
		carIdList.add(Long.parseLong(shoppingCarId));
		if(otherShoppingCarId != null){
			String[] carIdArray = otherShoppingCarId.split(Constants.ID_SEPARATOR);
			for (String id : carIdArray) {
				carIdList.add(Long.parseLong(id));
			}
		}
		
		return shoppingCarService.deleteShoppingCar(carIdList);
	}

}
